/*\
 * Copyright 2012 Rob van de Werken
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
\*/

package nl.RvdW.TVgids;

import java.util.ArrayList;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ChannelAdapter extends ArrayAdapter<Programme> {
	private ArrayList<Programme> programs;

	public ChannelAdapter(Context context, int textViewResourceId, ArrayList<Programme> programs) {
		super(context, textViewResourceId, programs);
		this.programs = programs;
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View v = convertView;
		if (v == null) {
			LayoutInflater vi = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			v = vi.inflate(R.layout.listitem_channel, null);
		}

		Programme p = programs.get(position);
		if (p != null) {
			v.setId(p.id);

			ImageView icon = (ImageView) v.findViewById(R.id.channel);
			TextView title = (TextView) v.findViewById(R.id.title);
			TextView start = (TextView) v.findViewById(R.id.start);
			TextView stop = (TextView) v.findViewById(R.id.stop);

			if (icon != null) {
				icon.setImageResource(Global.getDrawableId("channel_"+Integer.toString(p.channel)));
			}
			if (title != null) {
				title.setText(p.title);
			}

			if (start != null) {
				start.setText(Global.dateformat.format(p.start));
			}
			if (stop != null) {
				stop.setText(Global.dateformat.format(p.stop));
			}
		}
		return v;
	}
}
